package com.qfedu.edu.controller;

import com.qfedu.edu.pojo.User;
import com.qfedu.edu.result.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaobobo
 * @title: UserController
 * @projectName CD-Java-FY-2401-Springboot-Parent
 * @description: TODO
 * @date 2024/9/18  14:30
 */
@RestController
@Api(tags = "用户模块", description = "用户模块")
@Slf4j
public class UserController {


    /**
     * 请求地址
     * 请求方法
     * 求求接口说明
     * 响应参数说明搞定了
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有用户接口", notes = "不用传递参数")
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = User.class),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class)
    })
    public R list() {
        List<User> userList = new ArrayList<>();
        return R.ok().data("userList", userList);
    }


    /**
     * 通过用户名和密码查询用户对象
     *
     * @param username
     * @param password
     * @return
     * @ApiImplicitParam ：这个表示的是一个参数的说明
     * @ApiImplicitParams：代表的是多个参数的说明
     *    name：参数名字
     *    value：参数含义
     *    required：参数是否必须传递
     *    dataType：参数的类型是啥
     *    paramType：参数传输的形式
     *            query：正常的键值对的传输   如果是get 直接键值对 (直接传递键值对就下面的接受方式接受)
     *            http://127.0.0.1:8080/listUserByUsernameAndPassword?username=xxx&password=123
     */
    @GetMapping("/listUserByUsernameAndPassword")
    @ApiOperation(value = "通过用户名和密码查询用户对象接口", notes = "用户名和密码不能为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query")
    }
    )
    //还需要对这个响应的状态码进行说明
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = User.class),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class),
            @ApiResponse(code = 100, message = "参数有误", response = R.class),
            @ApiResponse(code = 103, message = "系统异常", response = R.class)
    })
    public R listUserByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password) {
        log.info("用户名是:{}---密码是:{}",username,password);
        return R.ok().data("user",new User(1,"中国好","确实好"));
    }


    /**
     *
     *  参数直接放到地址中的传递方式
     * http://127.0.0.1:8080/listUserByUsernameAndPassword1/xiaobobo/123
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/listUserByUsernameAndPassword1/{username}/{password}")
    @ApiOperation(value = "通过用户名和密码查询用户对象接口1", notes = "用户名和密码不能为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "path")
    }
    )
    //还需要对这个响应的状态码进行说明
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = User.class),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class),
            @ApiResponse(code = 100, message = "参数有误", response = R.class),
            @ApiResponse(code = 103, message = "系统异常", response = R.class)
    })
    public R listUserByUsernameAndPassword1(@PathVariable("username") String username, @PathVariable("password") String password) {
        log.info("用户名是:{}---密码是:{}",username,password);
        return R.ok().data("user",new User(1,"中国好","确实好"));
    }

    /**
     *
     * 请求头中包含token这个参数
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/listUserByUsernameAndPassword3/{username}/{password}")
    @ApiOperation(value = "通过用户名和密码查询用户对象接口2", notes = "用户名和密码不能为空")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "token",value = "用户令牌",required = true,dataType = "string",paramType = "header")  //这个就表示的是参数放到了请求头中
    }
    )
    //还需要对这个响应的状态码进行说明
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = User.class),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class),
            @ApiResponse(code = 100, message = "参数有误", response = R.class),
            @ApiResponse(code = 103, message = "系统异常", response = R.class)
    })
    public R listUserByUsernameAndPassword2(@PathVariable("username") String username, @PathVariable("password") String password, HttpServletRequest req) {
        log.info("用户名是:{}---密码是:{}", username, password);
        String token = req.getHeader("token");
        log.info("token是:{}", token);
        return R.ok().data("user", new User(1, "中国好", "确实好"));
    }


    /**
     * 传递键值对的写法
     *   如果直接传递对象的话 这里的参数是不需要描述的
     * @param user
     * @return
     */
    @PostMapping("addUser")
    @ApiOperation("添加用户")
    //还需要对这个响应的状态码进行说明
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = R.class,
                    responseHeaders ={
                    @ResponseHeader(name = "kkkk1",description = "哈哈哈哈")}
            ),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class),
            @ApiResponse(code = 100, message = "参数有误", response = R.class),
            @ApiResponse(code = 103, message = "系统异常", response = R.class)
    })
    @ApiImplicitParam(name = "token",value = "用户令牌",required = true,dataType = "string",paramType = "header")  //这个就表示的是参数放到了请求头中
    public R addUser(User user){
        log.info("传递的对象是:{}",user);
        return R.ok();
    }

    /**
     * 下面测试的是传递JSON格式的数据
     * @param user
     * @return
     */
    @PostMapping("addUser1")
    @ApiOperation("添加用户")
    //还需要对这个响应的状态码进行说明
    @ApiResponses({
            @ApiResponse(code = 0, message = "请求成功", response = R.class),
            @ApiResponse(code = 401, message = "认证失败", response = R.class),
            @ApiResponse(code = 403, message = "没有权限访问", response = R.class),
            @ApiResponse(code = 100, message = "参数有误", response = R.class),
            @ApiResponse(code = 103, message = "系统异常", response = R.class)
    })
    @ApiImplicitParam(name = "token",value = "用户令牌",required = true,dataType = "string",paramType = "header")  //这个就表示的是参数放到了请求头中
    public R addUser1(@RequestBody User user){
        log.info("传递的对象是:{}",user);
        return R.ok();
    }

}
